package com.splider.toutiao;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
	public MD5Util() {
	}

	public static byte[] md5(byte[] in) {
		MessageDigest md = null;
		byte[] out = null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(in);
			out = md.digest();
		} catch (NoSuchAlgorithmException ex) {
			System.out.println(ex.toString());
		}
		return out;
	}

	public static byte[] md5(byte[] in, int len) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException ex) {
			System.out.println(ex.toString());
		}
		md.reset();
		md.update(in, 0, len);
		byte[] out = md.digest();
		return out;
	}

	public static String MD5Str(String str) {
		StringBuffer buf = new StringBuffer("");
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();

			int i;
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return buf.toString();
	}

}
